﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace RayDen.Library.Entity.Scene {

    [Serializable]
    public class DefaultMaterialContainer : IMaterialProvider, IDictionary<string,MaterialInfo> {
        private Dictionary<string, MaterialInfo> materials;
        private MaterialInfo defaultMaterial;

        public DefaultMaterialContainer(MaterialInfo mt, IEnumerable<MaterialInfo> mats ) {
            this.defaultMaterial = mt;
            this.materials = mats == null ? new Dictionary<string, MaterialInfo>() : mats.ToDictionary(KeySelector);
        }

        private string KeySelector(MaterialInfo materialInfo) {
            return materialInfo.Name.ToLowerInvariant();
        }

        public MaterialInfo[] GetAll() {
            return this.materials.Values.ToArray();
        }

        public MaterialInfo Get(int id)
        {
            throw new NotImplementedException();
        }

        public MaterialInfo Get(string name) {
            MaterialInfo result = null;
            if (materials.TryGetValue(name, out result)) {//.ToLower()
                return result;
            }
            return defaultMaterial;
        }

        #region IDictionary<string,MaterialInfo> Members

        public void Add(string key, MaterialInfo value) {
            this.materials.Add(key,value);
        }

        public bool ContainsKey(string key) {
            return materials.ContainsKey(key);
        }

        public ICollection<string> Keys {
            get {
                return materials.Keys;
            }
        }

        public bool Remove(string key) {
            return materials.Remove(key);
        }

        public bool TryGetValue(string key, out MaterialInfo value) {
            return materials.TryGetValue(key, out value);
        }

        public ICollection<MaterialInfo> Values {
            get { return materials.Values; }
        }

        public MaterialInfo this[string key] {
            get {
                return materials[key];
            }
            set { materials[key] = value; }
        }

        #endregion

        #region ICollection<KeyValuePair<string,MaterialInfo>> Members

        public void Add(KeyValuePair<string, MaterialInfo> item) {
            materials.Add(item.Key, item.Value);
        }

        public void Clear() {
            materials.Clear();
        }

        public bool Contains(KeyValuePair<string, MaterialInfo> item) {
            return materials.ContainsKey(item.Key);
        }

        public void CopyTo(KeyValuePair<string, MaterialInfo>[] array, int arrayIndex) {}

        public int Count {
            get { return materials.Count; }
        }

        public bool IsReadOnly {
            get { return true; }
        }

        public bool Remove(KeyValuePair<string, MaterialInfo> item) {
            return this.materials.Remove(item.Key);
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,MaterialInfo>> Members

        public IEnumerator<KeyValuePair<string, MaterialInfo>> GetEnumerator() {
            return materials.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return materials.GetEnumerator();
        }

        #endregion
    }
}